<?php

namespace App\Recharge\Api;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Xiyou extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $array = [
            'GID'   => $bean->getGoodsId(), //商品编码
            'ID'    => $bean->getSupplierNum(), //商户密钥
            'Moery' => bcmul(strval($bean->getUnitPrice()), strval($bean->getStartCharging()), 4),
            'Num'   => $bean->getRechargeNum() * $bean->getStartCharging(), //充值数量,
            'PH'    => $bean->getAccount(),
            'OID'   => $bean->getOrderNo(), //订单号
            'TIM'   => time(),
        ];
        ksort($array);
        $signStr = urldecode(http_build_query($array));
        $array['SG'] = strtoupper(md5($signStr));
        $array['Callback'] = $bean->getNotifyUrl(); //异步回调地址

        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('code', '');
        $order_sn = $request->input('OID', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['1006', '1007']))->setRechargeSuccess($status == '1006');
    }

    /**
     * @return mixed
     */
    public function apiNotifySuccess(): mixed
    {
        return response('yes');
    }
}
